package com.youko.backstage.service.commodity;

import com.youko.backstage.bean.commodity.*;
import com.youko.backstage.mapper.commodity.*;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: youko
 * @classname: CommoditySpuService
 * @Description: 商品Spu service层
 * @date: 2022/7/5 16:22
 */
@Service
public class CommoditySpuService {
    @Autowired
    CommoditySpuMapper commoditySpuMapper;

    @Autowired
    CommoditySpecMapper commoditySpecMapper;

    @Autowired
    CommoditySpecValueMapper commoditySpecValueMapper;

    @Autowired
    CommoditySpuSpecMapper commoditySpuSpecMapper;

    @Autowired
    PicturePathMapper picturePathMapper;








    /**
     * 新增Spu商品类
     */
    public int insertCommoditySpu(CommoditySpu commoditySpu){
        commoditySpuMapper.insertSpu(commoditySpu);
        int spuId=commoditySpu.getId();
        return spuId;
    }

    /**
     * 新增一个spec组
     */
    public int insertSpec(CommoditySpec commoditySpec,String[] specValue){
        commoditySpecMapper.insertSpec(commoditySpec);
        int specId=commoditySpec.getId();
        int[] specValueIds=new int[3];
        for (int i=0;i<specValue.length;i++){
            CommoditySpecValue commoditySpecValue=new CommoditySpecValue();
            commoditySpecValue.setSpecId(specId);
            commoditySpecValue.setSpecValue(specValue[i]);
            commoditySpecValueMapper.insertSpecValue(commoditySpecValue);
            specValueIds[i] = commoditySpecValue.getId();
        }
        return specId;
    }


    /**
     * 为指定的Spu增加一个Spec组
     */
    public void insertSpuSpec(int spuId,int specId){
        CommoditySpuSpec commoditySpuSpec = new CommoditySpuSpec();
        commoditySpuSpec.setSpuId(spuId);
        commoditySpuSpec.setSpecId(specId);
        commoditySpuSpecMapper.insertSpuSpec(commoditySpuSpec);
    }

    /**
     *返回spu拥有的SpecValue
     */
    public List<CommoditySpecValue> getSpecValues(int spuId){
        List<CommoditySpuSpec> commoditySpuSpecs = commoditySpuSpecMapper.selectSpuSpecBySpuId(spuId);
        List<CommoditySpecValue> commoditySpecValues=new ArrayList<CommoditySpecValue>();
        for (CommoditySpuSpec css:commoditySpuSpecs){
            int specId = css.getSpecId();
            commoditySpecValues.addAll(commoditySpecValueMapper.selectSpecValueBySpecId(specId));
        }
        return commoditySpecValues;


    }


    /**
     * 插入图片路径
     */
    public void insertPicturePath(PicturePath picturePath){
        picturePathMapper.insertPicturePath(picturePath);
    }


    /**
     * 根据spuID查spu
     */
    public CommoditySpu selectSpuById(int spuId){
        return commoditySpuMapper.selectSpu(spuId);
    }

    public void deleteSpu(int spuId){
        commoditySpuMapper.deleteSpu(spuId);
    }
}
